package com.yunshang.shoppingmall.ShopTask.Task.Hsys;

import com.alibaba.fastjson.JSON;
import com.yunshang.shoppingmall.Methods.OrderMethods;
import com.yunshang.shoppingmall.Methods.ProductMethods;
import com.yunshang.shoppingmall.ShopTask.ShopState;
import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.entity.Config;
import com.yunshang.shoppingmall.entity.Hsys.HsysOrderData;
import com.yunshang.shoppingmall.entity.Hsys.HsysOrderRoot;
import com.yunshang.shoppingmall.entity.ShopTaskState;

import java.text.SimpleDateFormat;
import java.util.*;

public class HsysOrder {

//    public Config config= ConfigMethods.getConfig();
//    public String UserUrl= config.getUserUrl();
//    public String key=config.getUserKey();

    private List<HsysOrderData> hsysOrderList;
    private List<String> orderNoList;
    private List<String> productList;

    Config config=null;

    public void getOrder() {
        ShopState.SetMsg("荟生云商订单同步","开始读取荟生云商数据",0,0,0,0,0);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date t1 = c.getTime();
        String endTime =formatter.format(c.getTime());
        c.add(Calendar.HOUR_OF_DAY, -3);
        Date t2 = c.getTime();
        String startTime =formatter.format(c.getTime());
        String hsysUrl = "http://yc.huisheng2688.com/hsycMgr/app/getUserOrder.do";
        String param = "starttime=" + startTime
                + "&endtime=" + endTime
                + "&encryptStr=60f5a3c2ab917609282ebaafc6365589"
                + "&clientIp=127.0.0.1";
        System.out.println(param);
        /*
        String json = HttpRequest.sendPost(hsysUrl,param);
        */
        String json ="{\"type\":\"1\",\"msg\":null,\"data\":[{\"userno\":\"H8166\",\"level\":2,\"angeluserno\":\"c931827\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"马海山\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540534249000,\"lxrName\":\"马海山\",\"lxrDh\":\"13239508166\",\"address\":\"宁夏回族自治区银川市兴庆区新安家园西区16楼三单元1701号\",\"province\":\"宁夏回族自治区\",\"city\":\"银川市\",\"country\":null,\"area\":\"兴庆区\",\"list\":[{\"id\":13647,\"internalid\":\"ITEM1540534248884dff6089f\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD154053424888167c7c602\",\"goodsName\":\"双清芦荟口服液(4瓶)\",\"goodsFullName\":\"双清芦荟口服液(4瓶)\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1536926163172%E5%8F%8C%E6%B8%85660.jpg\",\"createTime\":\"2018-10-26 14:10:48\",\"goodsId\":256,\"shopcarid\":61289,\"quantity\":1,\"price\":660.0,\"pricepv\":100.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H483927074\",\"level\":2,\"angeluserno\":\"H821457604\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"胡敏\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540534505000,\"lxrName\":\"胡敏\",\"lxrDh\":\"18208261455\",\"address\":\"四川省宜宾市筠连县仁和街13号（妈咪宝贝仓库）\",\"province\":\"四川省\",\"city\":\"宜宾市\",\"country\":null,\"area\":\"筠连县\",\"list\":[{\"id\":13648,\"internalid\":\"ITEM1540534504898b8a260b3\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD1540534504896cd18f2a5\",\"goodsName\":\"骨营养素（1盒）\",\"goodsFullName\":\"骨营养素（1盒）\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1531392458583%E9%92%99.jpg\",\"createTime\":\"2018-10-26 14:15:04\",\"goodsId\":235,\"shopcarid\":61290,\"quantity\":1,\"price\":660.0,\"pricepv\":75.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H216784\",\"level\":2,\"angeluserno\":\"A112839\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"李绍宏\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540537153000,\"lxrName\":\"李绍宏\",\"lxrDh\":\"18177216784\",\"address\":\"广西壮族自治区柳州市市辖区柳太路西一巷1一9号瀚杰汽配厂\",\"province\":\"广西壮族自治区\",\"city\":\"柳州市\",\"country\":null,\"area\":\"市辖区\",\"list\":[{\"id\":13649,\"internalid\":\"ITEM1540537152520f88aefa0\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD1540537152517f592f3d5\",\"goodsName\":\"茶多酚(1条)[5盒30包]+骨营养素(1瓶)\",\"goodsFullName\":\"茶多酚(1条)[5盒30包]+骨营养素(1瓶)\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1537084036095%E8%8C%B6%E5%A4%9A%E9%85%9A%E5%A5%97%E9%A4%901.jpg\",\"createTime\":\"2018-10-26 14:59:12\",\"goodsId\":266,\"shopcarid\":61291,\"quantity\":1,\"price\":660.0,\"pricepv\":100.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H265076\",\"level\":2,\"angeluserno\":\"C655180\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"王新蕾\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540540193000,\"lxrName\":\"王新蕾\",\"lxrDh\":\"18369265076\",\"address\":\"山东省枣庄市薛城区山东省枣庄市薛城区北批市场南门\",\"province\":\"山东省\",\"city\":\"枣庄市\",\"country\":null,\"area\":\"薛城区\",\"list\":[{\"id\":13650,\"internalid\":\"ITEM154054019277392543ca1\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD1540540192771f5a2fee1\",\"goodsName\":\"双清芦荟口服液(4瓶)\",\"goodsFullName\":\"双清芦荟口服液(4瓶)\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1536926163172%E5%8F%8C%E6%B8%85660.jpg\",\"createTime\":\"2018-10-26 15:49:52\",\"goodsId\":256,\"shopcarid\":61293,\"quantity\":1,\"price\":660.0,\"pricepv\":100.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H940470260\",\"level\":2,\"angeluserno\":\"c032965\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"高建民\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540541520000,\"lxrName\":\"高建民\",\"lxrDh\":\"18031683200\",\"address\":\"河北省廊坊市安次区光明西道119号\",\"province\":\"河北省\",\"city\":\"廊坊市\",\"country\":null,\"area\":\"安次区\",\"list\":[{\"id\":13651,\"internalid\":\"ITEM154054152040814ed2db5\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD1540541520406eb7399fe\",\"goodsName\":\"骨营养素（1盒）\",\"goodsFullName\":\"骨营养素（1盒）\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1531392458583%E9%92%99.jpg\",\"createTime\":\"2018-10-26 16:12:00\",\"goodsId\":235,\"shopcarid\":61295,\"quantity\":1,\"price\":660.0,\"pricepv\":75.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H456048\",\"level\":2,\"angeluserno\":\"c149995\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"王学瑞\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540541794000,\"lxrName\":\"王学瑞\",\"lxrDh\":\"18335456048\",\"address\":\"山西省晋中市榆次区安宁东街体育西路泰悦华庭3-1-702\",\"province\":\"山西省\",\"city\":\"晋中市\",\"country\":null,\"area\":\"榆次区\",\"list\":[{\"id\":13652,\"internalid\":\"ITEM15405417944830e308246\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD15405417944813ce12037\",\"goodsName\":\"双清芦荟口服液(4瓶)\",\"goodsFullName\":\"双清芦荟口服液(4瓶)\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1536926163172%E5%8F%8C%E6%B8%85660.jpg\",\"createTime\":\"2018-10-26 16:16:34\",\"goodsId\":256,\"shopcarid\":61274,\"quantity\":1,\"price\":660.0,\"pricepv\":100.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H1780\",\"level\":2,\"angeluserno\":\"H2168\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"李跃矿\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540543835000,\"lxrName\":\"许洪云\",\"lxrDh\":\"13337803302\",\"address\":\"江苏省南京市玄武区锁金村200号8幢2室\",\"province\":\"江苏省\",\"city\":\"南京市\",\"country\":null,\"area\":\"玄武区\",\"list\":[{\"id\":13653,\"internalid\":\"ITEM1540543835025ef56c160\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD154054383502220965eaf\",\"goodsName\":\"骨营养素（1盒）\",\"goodsFullName\":\"骨营养素（1盒）\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1531392458583%E9%92%99.jpg\",\"createTime\":\"2018-10-26 16:50:35\",\"goodsId\":235,\"shopcarid\":61282,\"quantity\":1,\"price\":660.0,\"pricepv\":75.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0},{\"userno\":\"H078988\",\"level\":2,\"angeluserno\":\"c115\",\"clientIp\":\"127.0.0.1\",\"encryptStr\":\"60f5a3c2ab917609282ebaafc6365589\",\"username\":\"张德弟\",\"money_total\":660.0,\"usegwbmoney\":0.0,\"usezspointmoney\":0.0,\"paymoney\":660.0,\"createtime\":null,\"paysuccesstime\":1540544015000,\"lxrName\":\"刘莉\",\"lxrDh\":\"15856546076\",\"address\":\"贵州省贵阳市云岩区延安西路瀑布大厦1701。\",\"province\":\"贵州省\",\"city\":\"贵阳市\",\"country\":null,\"area\":\"云岩区\",\"list\":[{\"id\":13654,\"internalid\":\"ITEM1540544015277f7d08c9b\",\"teamcode\":null,\"teamname\":null,\"orderID\":\"DD1540544015275db7d5c47\",\"goodsName\":\"骨营养素（1盒）\",\"goodsFullName\":\"骨营养素（1盒）\",\"goodsImage\":\"http://image-10049102.file.myqcloud.com/greencreator/1531392458583%E9%92%99.jpg\",\"createTime\":\"2018-10-26 16:53:35\",\"goodsId\":235,\"shopcarid\":61299,\"quantity\":1,\"price\":660.0,\"pricepv\":75.0,\"stamppoint\":0.0,\"deliverystate\":0,\"logisticsno\":null,\"merchantid\":0,\"deliveryuserid\":null,\"deliverytime\":null}],\"type\":3,\"starttime\":\"2018-10-26 13:58:00\",\"endtime\":\"2018-10-26 16:58:00\",\"getBySelf\":0}],\"param\":null,\"success\":true}\n";
        System.out.println(json);

        HsysOrderRoot hsysOrderRoot =  JSON.parseObject(json, HsysOrderRoot.class);
        hsysOrderList = hsysOrderRoot.getData();
        ShopState.SetMsg("荟生云商订单同步","荟生云商数据读取完成",hsysOrderList.size(),0,0,0,0);
        orderNoList = OrderMethods.getOrderNoList(t1,t2);
        ShopState.SetMsg("荟生云商订单同步","新零售微店订单数据读取完成",orderNoList.size(),0,0,0,0);
        productList = ProductMethods.getProductList();
        ShopState.SetMsg("荟生云商订单同步","新零售微店商品数据读取完成",productList.size(),0,0,0,0);

        total = hsysOrderList.size();
        location=0;
        suc=0;
        fail=0;
        skip=0;

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                HsysData();
            }
        }, 1000);

    }


    private int total=0;
    private int location=0;
    private int suc=0;
    private int fail=0;
    private int skip=0;

    public void HsysData() {
        if (location < total) {

            ShopState.SetMsg("荟生云商订单同步", "开始处理数据", total, location + 1, suc, fail, skip);

            HsysOrderData hsysOrderData = hsysOrderList.get(location);
            //判断订单编号是否存在,不存在
            String OrderID = hsysOrderData.getList().get(0).getOrderID();
            if (orderNoList.indexOf(OrderID) == -1) {
                ShopState.SetMsg("荟生云商订单同步", "开始处理订单"+OrderID, total, location + 1, suc, fail, skip);

                //添加商品
                //添加规格
                //价格
                String productName = hsysOrderData.getList().get(0).getGoodsName();
                if(productList.indexOf(productName) == -1) //商品不存在，添加商品
                {

                }

                //添加购物车
                //添加订单
                suc++;
            }
            else
            {
                skip++;
            }

            location++;
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    HsysData();
                }
            }, 1000);
        }
        else
        {
            ShopState.SetMsg("荟生云商订单同步","完成",total,location,suc,fail,skip);
            ShopTaskState.setComplete(false);
        }
    }
}
